package com.newtouch.krd_app.util;

/**
 * author : TQ
 * time   : 2023/2/19
 * desc   : 异常捕获类
 */

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.hjq.toast.ToastUtils;
import com.jakewharton.processphoenix.ProcessPhoenix;
import com.newtouch.krd_app.BuildConfig;
import com.newtouch.krd_app.KrdApplication;
import com.newtouch.krd_app.MainActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

public class CrashHandler implements Thread.UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    KrdApplication application;
    private static CrashHandler mInstance;
    private boolean isdebug = false;
    /**
     * 单列模式获取
     * @return
     */
    public static synchronized CrashHandler getInstance(){
        if (mInstance == null){
            mInstance = new CrashHandler();
        }
        return mInstance;
    }
    public CrashHandler() {

    }
    public CrashHandler(KrdApplication myApplication) {
        // // 获取系统默认的UncaughtException处理器
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        this.application = myApplication;
    }

    public void init(KrdApplication myApplication){
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        this.application = myApplication;
    }

    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
//        ProcessPhoenix.triggerRebirth(application.getApplicationContext());
        towriteLog(ex);
    }

    private void towriteLog(Throwable ex){
        FileWriter fileWriter = null;
        File file1=new File(application.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+"/Log.txt");
        try {
            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            ex.printStackTrace(printWriter);
            Throwable cause = ex.getCause();
            while (cause != null) {
                cause.printStackTrace(printWriter);
                cause = cause.getCause();
            }
            printWriter.close();
            String result = writer.toString();

            fileWriter = new FileWriter(file1.getPath(),true);
            fileWriter.write(result);
            fileWriter.flush();
            if (fileWriter!=null){
                fileWriter.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    public void towriteLog(String msg){
        if (isdebug){
            FileWriter fileWriter = null;
            File file1=new File(application.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+"/Log.txt");
            try {
                fileWriter = new FileWriter(file1.getPath(),true);
                fileWriter.write(msg+"\n");
                fileWriter.flush();
                if (fileWriter!=null) fileWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
